package ltd.followkba.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.Timer;
import java.util.TimerTask;

import ltd.advskin.C;
import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.base.KcListener;
import ltd.advskin.media.Player;
import ltd.advskin.view.KcBtnImage;
import ltd.advskin.view.KcLyricsView;
import ltd.followkba.R;

import static ltd.advskin.MSKIN.MSTRING;

public class PlayerView extends ConstraintLayout {
    private KcBtnImage kbiPausePlay;
    private KcListener mKcListener;
    public Player mPlayer;
    private Timer mTimer;


    public PlayerView(Context context) {
        super(context);
        initView(context);
    }
    public PlayerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    public PlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.view_player, this, true);
        kbiPausePlay = findViewById(R.id.kbiPausePlay);
//        kbiPausePlay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                MSKIN.setBitmap(mCKcPlayer.isPlaying() ? C.play_play : C.play_pause, kbiPausePlay);
//                if (mKcListener != null){
//                    mKcListener.onMessage(false);
//                }
//            }
//        });

//        MSKIN.setBitmap(C.play_play, kbiPausePlay);
        View kbtCurrTimePlay = findViewById(R.id.kbtCurrPlay);  // 开始时间
        View kbtTotalTimePlay = findViewById(R.id.kbtTotalPlay);  // 总时间
        KcLyricsView klvLyricsPlay = findViewById(R.id.klvLyricsPlay); // 歌词

        mPlayer = MSKIN.startPlayer();
        mPlayer.setView(kbtCurrTimePlay, kbtTotalTimePlay, null, klvLyricsPlay);
        mPlayer.setView(kbiPausePlay, null);

//        mTimer = new Timer();
//        mTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                if (mPlayer != null){
//                    mPlayer.onTimer();
//                }
//            }
//        }, 100, 200);

    }
    public void setListener(KcListener kcListener){
        mKcListener = kcListener;
    }
    public void setKclFile(String kclFile) {
        mPlayer.setLyricsFile(kclFile);
    }
    public void playStart(int timeLength, String fileName, String lyricsFile) {       // 播放时如果已经有总的播放时间，从这里输入模块里面就不再计算了
        if (mPlayer == null || fileName == null || "".equals(fileName)) {
            return;
        }
        mPlayer.setTimeLength(timeLength);
        mPlayer.setPlayFile(fileName, lyricsFile);
//        MSKIN.setBitmap(C.mini_pause, kbiPausePlay);
    }
    public void restartPlay(){
//        if (mPlayer != null) {
//            mPlayer.restartPlay();
//        }
//        MSKIN.setBitmap(C.mini_pause, kbiPausePlay);
    }
    public void setPausePlay(){
        if (mPlayer != null) {
//            mPlayer.setPausePlay();
        }
        MSTRING(String.format("KcMiniPlayView setPausePlay PPPP %s", isPlaying()));
        MSKIN.setBitmap(isPlaying() ? C.mini_pause : C.mini_play, kbiPausePlay);
    }
    public void setStop(int timeLength){
        if (mPlayer != null) {
//            if (mPlayer.gTotalTime == 0){
//                mPlayer.gTotalTime = timeLength;
//            }
//            mPlayer.mStatus = PlayOldStatus.PLAYING;
//            mPlayer.setStop(false);
        }
        MSKIN.setBitmap(C.mini_play, kbiPausePlay);
    }

    public boolean isPlaying(){
        return mPlayer != null ? mPlayer.isPlaying() : false;
    }
    public boolean isStoping(){
        return false;//lmPlayer != null ? mPlayer.getS() : false;
    }
    public int getCurrentTime(){
        return mPlayer != null ? mPlayer.getCurrentTime() : 0;
    }
    public String turnTime(int time) {
        return "";//mPlayer != null ? mPlayer.turnTime(time) : "";
    }
    public void release(){
        MSKIN.stopPlayer();
//        if (mPlayer != null) {
//            mPlayer.setStop(false);
////            mPlayer.release();
//        }
//        if (mTimer != null) {
//            mTimer.cancel();
//        }
    }

}




